package com.zxmr.hospital.info.action;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.ConsultLevel;
import com.zxmr.domain.service.ConsultService;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.UserTraceManager;
import com.zxmr.util.Utils;

public class ConsultLevelAction extends Executor{

	enum method{
		add,
		delete,
		queryAll,
		update;
	}
	@Override
	public void execute(Request request, Response response) {
		// TODO Auto-generated method stub
		method m = Enum.valueOf(method.class, this.getElement().getMethod());
		try
		{
			switch(m)
			{
			case add:
				add(request,response);
				break;
			case queryAll:
				queryAll(request,response);
				break;
			case update:
				update(request,response);
				break;
			case delete:
				delete(request,response);
				break;
			}
		}catch(Exception e)
		{
			if(e instanceof ExecuteException)
			{
				ResponsePayload payload = new ResponsePayload(e.getMessage());
				response.setPayload(payload);
				response.setResult(ResultCode.COMPONENT_ERROR);
			}else
			{
				e.printStackTrace();
				this.internalErrorResponse(response);
			}
		}
	}
	
	public void add(Request request,Response response)throws ExecuteException
	{
		String name = request.getRequestParameters().get("name");
		if(Utils.isStringEmptyWithTrim(name))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.noname"));
		
		ConsultLevel ct = new ConsultLevel();
		ct.setCompany(request.getAccount().getCompany());
		ct.setName(name);
		ct.setDescription(request.getRequestParameters().get("description"));
		ConsultService service = new ConsultService();
		if(!service.addConsultLevel(ct))
			this.internalErrorResponse(response);
		else
		{
			String action = ResourceManager.getInstance().getMessage("com.zxmr.info.addConsultLevel",name);
			UserTraceManager.addTrace(action, request.getAccount());
		}
		
	}
	public void queryAll(Request request,Response response)throws ExecuteException
	{
		ConsultService service = new ConsultService();
		List<ConsultLevel> cts = service.queryAllConsultLevel(request.getAccount().getCompany());
		
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(cts));
		response.setPayload(payload);
	}
	public void delete(Request request,Response response)throws ExecuteException
	{
		int id = 0;
		try
		{
			id = Integer.parseInt(request.getRequestParameters().get("id"));
		}catch(Exception e)
		{
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.errorPara"));
		}
		
		ConsultService service = new ConsultService();
		ConsultLevel ct = service.queryConsultLevel(id);
		
		if(ct == null || ct.getCompany() != request.getAccount().getCompany())
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.noauth"));
		
		if(!service.deleteConsultLevel(id))
			this.internalErrorResponse(response);
		else
		{
			String action = ResourceManager.getInstance().getMessage("com.zxmr.info.deleteConsultLevel",ct.getName());
			UserTraceManager.addTrace(action, request.getAccount());
		}
	}
	public void update(Request request,Response response)throws ExecuteException
	{
		String name = request.getRequestParameters().get("name");
		if(Utils.isStringEmptyWithTrim(name))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.noname"));
		
		int id = 0;
		try
		{
			id = Integer.parseInt(request.getRequestParameters().get("id"));
		}catch(Exception e)
		{
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.errorPara"));
		}
		ConsultService service = new ConsultService();
		
		ConsultLevel ct = service.queryConsultLevel(id);
		if(ct == null || ct.getCompany() != request.getAccount().getCompany())
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.info.noauth"));
		
		
		ct.setCompany(request.getAccount().getCompany());
		ct.setName(name);
		ct.setDescription(request.getRequestParameters().get("description"));
		
		if(!service.updateConsultLevel(ct))
			this.internalErrorResponse(response);
	}

}
